<?php

namespace plugin\cms\app\controller\admin;

use core\controller\AdminBase;
use Exception;
use plugin\cms\app\bll\Category as BllCategory;
use plugin\cms\app\bll\Content as BllContent;
use plugin\cms\app\bll\Model as BllModel;
use plugin\cms\app\bll\ModelField as BllModelField;
use plugin\cms\app\bll\ContentPending as BllContentPending;
use plugin\cms\app\bll\Tag as BllTag;
use plugin\cms\app\bll\TagContent as BllTagContent;
use plugin\cms\app\enum\ContentStatus;
use support\View;
use Webman\Event\Event;

class Index extends AdminBase
{

    public function index()
    {

        $bllCategory = new BllCategory();

        //查询文章
        if (request()->get('submittype', '') == 'selcontents') {

            $bllContent = new BllContent();
            $getdata    = request()->get();
            unset($getdata['submittype']);

            $getdata['siteid']   = request()->nowsiteid;
            $getdata['pagesize'] = 10;
            $contents = $bllContent->paginate(
                ...$getdata
            );

            //获取所有TAG
            $bllTag        = new BllTag();
            $alltags       = $bllTag->select(siteid: request()->nowsiteid);
            $alltags       = array_column($alltags, null, 'id');
            $bllTagContent = new BllTagContent();

            foreach ($contents as $key => $content) {
                $tagids =  $bllTagContent->selTagidsByContentid($content->id);
                $tags = [];
                foreach ($tagids as $tagid) {
                    if (isset($alltags[$tagid])) {
                        $tags[] = $alltags[$tagid];
                    }
                }
                $contents[$key]->tags = $tags;
            }

            return $this->success(['items' => $contents->items(), 'total' => $contents->total()]);
        }

        if (request()->get('submittype', '') == 'selsearchfields') {
            //获取栏目信息
            $cateinfo = $bllCategory->find(
                request()->get('cid'),
                request()->nowsiteid
            );


            //获取查询字段
            $bllModelField = new BllModelField();
            $fields = $bllModelField->selSearchField($cateinfo['mid']);
            return $this->success($fields);
        }

        $cates       = $bllCategory->cascader(siteid: request()->nowsiteid, subtitle: 'publishedcount');

        View::assign('cates', $cates);
        return $this->view();
    }

    public function add()
    {
        if (is_post()) {
            try {
                $data           = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $data['uid']    = request()->admininfo['id'];
                $bllContent     = new BllContent();
                $bllContent->add($data, request()->nowsiteid);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {


            $cid           = request()->get('cid');
            $bllCategory   = new BllCategory();
            $cateinfo      = $bllCategory->find($cid, request()->nowsiteid);
            if (!$cateinfo) {
                throw new Exception('栏目错误');
            }
            $bllModel      = new BllModel();
            $modelinfo     = $bllModel->find($cateinfo['mid'], request()->nowsiteid);
            $bllModelField = new BllModelField();
            $fields        = $bllModelField->selField($modelinfo['id']);


            //标签
            $bllTag = new BllTag();
            $tags = $bllTag->select(siteid: request()->nowsiteid);

            View::assign('fields', $fields);
            View::assign('cateinfo', $cateinfo);
            View::assign('tags', $tags);

            return $this->view();
        }
    }


    public function edit()
    {
        $id         = request()->get('id');
        $cid        = request()->get('cid');
        $bllContent = new BllContent();
        if (is_post()) {
            try {
                $data           = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllContent     = new BllContent();
                $bllContent->edit($id, $data, request()->nowsiteid);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllCategory   = new BllCategory();
            $cateinfo      = $bllCategory->find($cid, request()->nowsiteid);
            if (!$cateinfo) {
                throw new Exception('栏目错误');
            }
            $bllModel      = new BllModel();
            $modelinfo     = $bllModel->find($cateinfo['mid'], request()->nowsiteid);
            $bllModelField = new BllModelField();
            $fields        = $bllModelField->selField($modelinfo['id']);

            //标签
            $bllTag = new BllTag();
            $tags = $bllTag->select(siteid: request()->nowsiteid);

            $contentinfo = $bllContent->find(id: $id, cid: $cid, siteid: request()->nowsiteid);

            //文章的tag
            $bllTagContent = new BllTagContent();
            $contenttagids = $bllTagContent->selTagidsByContentid(contentid: $id, cid: $cid, siteid: request()->nowsiteid);
            View::assign('fields', $fields);
            View::assign('cateinfo', $cateinfo);
            View::assign('tags', $tags);
            View::assign('contentinfo', $contentinfo);
            View::assign('contenttagids', $contenttagids);
            return $this->view();
        }
    }


    /**
     * 移动到其他分类
     *
     * @return mixed
     */
    public function moveCate()
    {
        if (is_post()) {

            try {
                $bllContent     = new BllContent();
                $bllContent->moveCate(
                    request()->post('contentids'),
                    request()->post('fromcid'),
                    request()->post('tocid'),
                    request()->nowsiteid
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $fromcid = request()->get('fromcid');
            $bllCategory = new BllCategory();
            $fromcateinfo = $bllCategory->find(id: $fromcid, siteid: request()->nowsiteid);
            $tocates = $bllCategory->cascaderAllowMid(
                siteid: request()->nowsiteid,
                allowmids: [$fromcateinfo['mid']]
            );

            View::assign('tocates', $tocates);
            return $this->view(frame: false);
        }
    }


    /**
     * 软删除 加入到回收站 
     *
     * @return void
     */
    public function joinRecycle()
    {
        try {
            $bllContent =  new BllContent();
            $bllContent->toRecycle(
                contentids: request()->input('contentids'),
                cid: request()->input('cid'),
                siteid: request()->nowsiteid
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    /**
     * 回收站文章
     *
     * @return void
     */
    public function recycles()
    {
        //查询文章
        if (request()->get('type', '') == 'selcontents') {

            $bllContent = new BllContent();
            $contents = $bllContent->paginate(
                cid: request()->get('cid'),
                siteid: request()->nowsiteid,
                status: ContentStatus::STATUS_RECYCLE->value
            );
            return $this->success(['items' => $contents->items(), 'total' => $contents->total()]);
        }

        $bllCategory = new BllCategory();
        $cates       = $bllCategory->cascader(siteid: request()->nowsiteid, subtitle: 'recyclecount');
        View::assign('cates', $cates);
        return $this->view();
    }


    /**
     * 从数据库恢复
     *
     * @return void
     */
    public function restore()
    {
        try {
            $bllContent =  new BllContent();
            $bllContent->restore(
                contentids: request()->input('contentids'),
                cid: request()->input('cid'),
                siteid: request()->nowsiteid
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    /**
     * 从回收站中彻底删除
     *
     * @return void
     */
    public function delete()
    {
        try {
            $bllContent =  new BllContent();
            $bllContent->delete(
                contentids: request()->input('contentids'),
                cid: request()->input('cid'),
                siteid: request()->nowsiteid
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    /**
     * 设置权重
     *
     * @return void
     */
    public function setWeight()
    {
        try {
            $bllContent =  new BllContent();
            $bllContent->setWeight(
                id: request()->input('id'),
                cid: request()->input('cid'),
                siteid: request()->nowsiteid,
                weight: request()->input('weight')
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }


    /**
     * 待审文章
     *
     * @return mixed
     */
    public function pending()
    {
        //查询文章
        if (request()->get('type', '') == 'selcontents') {

            $bllContent = new BllContent();
            $contents = $bllContent->paginate(
                cid: request()->get('cid'),
                siteid: request()->nowsiteid,
                status: ContentStatus::STATUS_PENDING->value
            );
            return $this->success(['items' => $contents->items(), 'total' => $contents->total()]);
        }


        $bllCategory = new BllCategory();
        $cates       = $bllCategory->cascader(siteid: request()->nowsiteid, subtitle: 'pendingcount');
        View::assign('cates', $cates);
        return $this->view();
    }

    /**
     * 审核文章
     *
     * @return mixed
     */
    public function doPending()
    {
        $id         = request()->get('id');
        $cid        = request()->get('cid');
        $bllContent = new BllContent();
        if (is_post()) {
            try {
                //先编辑再审核
                $contentformdata           = request()->post('contentform');
                $contentformdata['siteid'] = request()->nowsiteid;
                $bllContent     = new BllContent();
                $bllContent->edit($id, $contentformdata, request()->nowsiteid);
                //再审核
                $pendingform = request()->post('pendingform');
                $bllContentPending = new BllContentPending();
                $bllContentPending->pending(
                    contentid: $id,
                    cid: $cid,
                    siteid: request()->nowsiteid,
                    status: $pendingform['status'],
                    pendinguid: request()->admininfo['id'],
                    opinion: $pendingform['opinion']
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllCategory   = new BllCategory();
            $cateinfo      = $bllCategory->find($cid, request()->nowsiteid);
            if (!$cateinfo) {
                throw new Exception('栏目错误');
            }
            $bllModel      = new BllModel();
            $modelinfo     = $bllModel->find($cateinfo['mid'], request()->nowsiteid);
            $bllModelField = new BllModelField();
            $fields        = $bllModelField->selField($modelinfo['id']);

            //标签
            $bllTag = new BllTag();
            $tags = $bllTag->select(siteid: request()->nowsiteid);

            $contentinfo = $bllContent->find(id: $id, cid: $cid, siteid: request()->nowsiteid);

            //文章的tag
            $bllTagContent = new BllTagContent();
            $contenttagids = $bllTagContent->selTagidsByContentid(contentid: $id, cid: $cid, siteid: request()->nowsiteid);

            //下一篇
            $nextcontent = $bllContent->nextContent(id: $id, cid: $cid, siteid: request()->nowsiteid, status: 0);

            //上一篇
            $prevcontent = $bllContent->prevContent(id: $id, cid: $cid, siteid: request()->nowsiteid, status: 0);

            View::assign('fields', $fields);
            View::assign('cateinfo', $cateinfo);
            View::assign('tags', $tags);
            View::assign('contentinfo', $contentinfo);
            View::assign('contenttagids', $contenttagids);
            View::assign('nextcontent', $nextcontent);
            View::assign('prevcontent', $prevcontent);

            return $this->view(frame: false);
        }
    }

    /**
     * 批量审核
     *
     * @return mixed
     */
    public function batchPending()
    {
        try {
            $data = request()->post();
            $bllContentPending = new BllContentPending();
            $bllContentPending->batchPending(
                contentids: $data['contentids'],
                cid: $data['cid'],
                siteid: request()->nowsiteid,
                status: $data['status'],
                pendinguid: request()->admininfo['id']
            );
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
